package com.group9.handinhand.dao;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Repository;
import org.springframework.util.MultiValueMap;

import com.group9.handinhand.dao.model.SocialUser;
/**
 * Class for Social User Dao
 *
 */
@Repository
public class SocialUserDao extends BaseDaoImpl {

	public List<SocialUser> findByUserId(String userId) {
		return null;
	}

	public List<SocialUser> findByUserIdAndProviderId(String userId, String providerId) {
		return null;
	}

	public List<SocialUser> findByUserIdAndProviderUserIds(String userId, MultiValueMap<String, String> providerUserIds) {
		return null;
	}
	/**
	 * finds social user with user id
	 * @param userId
	 * @param providerId
	 * @param providerUserId
	 * @return
	 */
	public SocialUser get(String userId, String providerId, String providerUserId) {
		String queryString = "Select su from SocialUser as su inner join su.user user where user.email =:email and su.providerId =:providerId and su.providerUserId =:providerUserId";
		SocialUser socialUser =  executeQueryStringForObject(queryString, new String[]{"email","providerId","providerUserId"}, 
									new Object[]{userId, providerId, providerUserId});
		return socialUser;
	}
	/**
	 * 
	 * @param userId
	 * @param providerId
	 * @return
	 */
	public List<SocialUser> findPrimaryByUserIdAndProviderId(String userId, String providerId) {
		return null;
	}
	/**
	 * chooses the max rank using user id 
	 * @param userId
	 * @param providerId
	 * @return
	 */
	public Integer selectMaxRankByUserIdAndProviderId(String userId, String providerId) {
		String queryString = "Select coalesce(max(su.rank) + 1, 1) as rank from SocialUser su inner join su.user user where user.email =:email and su.providerId =:providerId";
		
		Integer rank = executeQueryStringForObject(queryString,new String[]{"email","providerId"}, 
				new Object[]{userId,providerId});
		
		return rank;
	}
	/**
	 * finds user ids using provider id and provider user id
	 * @param providerId
	 * @param providerUserId
	 * @return
	 */
	public List<String> findUserIdsByProviderIdAndProviderUserId(String providerId, String providerUserId) {
		String queryString = "Select distinct (su.user.email) from SocialUser su where su.providerId =:providerId and su.providerUserId =:providerUserId";
		List<String> localUserIds =  executeQueryString(queryString, new String[]{"providerId","providerUserId"}, 
									new Object[]{providerId,providerUserId});
		return localUserIds;
	}

	public List<String> findUserIdsByProviderIdAndProviderUserIds(String providerId, Set<String> providerUserIds) {
		return null;
	}
	/**
	 * saves the user as social user
	 * @param socialUser
	 * @return
	 */
	public SocialUser saveSocialUser(SocialUser socialUser) {
		save(socialUser);
		return socialUser;
	}
	/**
	 * updates user as social user
	 * @param socialUser
	 * @return
	 */
	public SocialUser updateSocialUser(SocialUser socialUser) {
		update(socialUser);
		return socialUser;
	}

}
